package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.controller.tool.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/system/minioImage")
public class MinioImageController {

    @Autowired
    private MinioUtil minioUtil;

    /**
     * 上传图片到MinIO
     */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file) {
        try {
            String fileName = minioUtil.upload(file);
            String url = minioUtil.getFileUrl(fileName);
            return AjaxResult.success("上传成功", url);
        } catch (Exception e) {
            return AjaxResult.error("上传失败: " + e.getMessage());
        }
    }

    /**
     * 获取图片外链
     */
    @GetMapping("/url")
    public AjaxResult getUrl(@RequestParam("fileName") String fileName) {
        try {
            String url = minioUtil.getFileUrl(fileName);
            return AjaxResult.success(url);
        } catch (Exception e) {
            return AjaxResult.error("获取URL失败: " + e.getMessage());
        }
    }
}
